package com.now.printer.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.now.printer.R;
import com.now.printer.adapter.menu.DrawerAdapter;
import com.now.printer.adapter.menu.DrawerItem;
import com.now.printer.adapter.menu.SimpleItem;
import com.now.printer.adapter.menu.SpaceItem;
import com.now.printer.utils.ResUtils;
import com.now.printer.widget.NBToolBar;
import com.xuexiang.xutil.resource.ThemeUtils;
import com.yarolegovich.slidingrootnav.SlideGravity;
import com.yarolegovich.slidingrootnav.SlidingRootNav;
import com.yarolegovich.slidingrootnav.SlidingRootNavBuilder;
import com.yarolegovich.slidingrootnav.callback.DragStateListener;

import java.util.Arrays;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xudong on 2021/6/2.
 */

public abstract class BaseFragment2 extends Fragment {

    protected boolean isVisiable = false;
    Unbinder unbinder;
    protected NBToolBar nbToolbar;

    private FrameLayout baseContainerFrameLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View convertView = inflater.inflate(R.layout.fragment_container_base, container, false);

        nbToolbar = convertView.findViewById(R.id.nb_toolbar);
        nbToolbar.setVisibility(isShowToolbar() ? View.VISIBLE : View.GONE);
        baseContainerFrameLayout = convertView.findViewById(R.id.base_container_frame_layout);
        baseContainerFrameLayout.addView(LayoutInflater.from(getActivity()).inflate(getLayoutId(),null));
        unbinder = ButterKnife.bind(this, convertView);
        doBusiness(getActivity(),convertView);

        return convertView;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getUserVisibleHint()) {
            isVisiable = true;
            onVisiable();
        } else {
            isVisiable = false;
            onInvisiable();
        }
    }

    protected void onVisiable() {
        onLazy();
    }

    protected abstract void onLazy();


    protected void onInvisiable() {

    }

    protected boolean isShowToolbar(){
        return true;
    }

    /**
     * [绑定布局]
     *
     * @return
     */
    public abstract int getLayoutId();

    /**
     * [业务操作]
     * 只执行一次
     *
     * @param mContext
     */
    public abstract void doBusiness(Context mContext, View convertView );

    /**
     * [页面跳转]
     *
     * @param clz
     */
    public void startActivity(Class<?> clz) {
        startActivity(new Intent(getActivity(), clz));
    }

    /**
     * [携带数据的页面跳转]
     *
     * @param clz
     * @param bundle
     */
    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    public void startActivityForResult(Class<?> clz,int i){
        Intent intent = new Intent();
        intent.setClass(getActivity(),clz);
        startActivityForResult(intent,i);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }





}
